const sequelize = require('../db.js')
const { QueryTypes } = require('sequelize');
const router = require('koa-router')()
router.prefix('/address')
router.get('/',async ctx=>{
    ctx.body = {
        code:200,
        data:await sequelize.query('select * from address',{ type: QueryTypes.SELECT })
    }
})
router.post('/add',async ctx=>{
    const {name,phone,address,ssq} = ctx.request.body
    try{
        const r = await sequelize.query(`insert into address values(null,'${name}','${phone}','${address}','${ssq}')`)
        ctx.body = {
            code:200,
            msg:'添加成功'
        }
    }catch{
        ctx.body = {
            code:500,
            msg:'添加失败'
        }
    }
    // console.log(r)
})
router.post('/update',async ctx=>{
	const {name,phone,address,ssq,id} = ctx.request.body
	const r = await sequelize.query(`update address set name='${name}',phone='${phone}',address='${address}',ssq='${ssq}' where id=${id}`)
	ctx.body = {
	    code:200,
	    msg:'修改成功'
	}
})
router.del('/del',async ctx=>{
const {id} = ctx.query
try{
    const r = sequelize.query(`delete from address where id=${id}`)
    ctx.body = {
        code:200,
        msg:'删除成功'
    }
}catch{
    ctx.body={
        code:500,
        msg:'请求失败'
    }
}
})
module.exports = router
